/**
 * Block.m
 *
 * 
 * Revision History: 
 *
 * Date     Author       Version   Changes
 * ======== ============ ========= ================================================================
 * 05/08/09 PJ Cabrera   1.0       Initial version.
 * 02/17/10 Steve Hu	 1.1	   Updated board coordinates to match our design.
 * 02/22/10 Robin Mahony 1.2	   Updated "color" variable to "type" to match our design.
 * 02/24/10 Robin Mahony 1.3	   Updated TransformAnchor point so blocks will fall within 
 *								   the correct area of our background.
 * 02/25/10 Robin Mahony 1.4	   Updated random number generator and BlockType switch statement
 *								   to use new graphics and new block types (integers instead of 
 *								   colors).
 * 03/03/10 Olive C.     1.5       Add boardX and boardY to get the position of current block.
 *                                 Change to only have 1~5 blocks for now.
 * 03/06/10 Steve Hu     1.6	   Changed variable "type" to the class variable.
 * 03/12/10 Robin Mahony 1.7	   Changed random generation of block values to 0-5, where 0 is 
 *								   least likely.
 * 03/18/10 Robin Mahony 1.8	   Added ability to unlock different blocks at different levels;
 *								   This is done by accepting the current level (lev) when "newBlock" is called
 *								   and using this level to control which blocks are available
 * 03/20/10 Robin Mahony 1.9	   Changed 0 blocks to appear at level 3, and negative blocks to start
 *								   appearing after level 5.
 * 03/20/10 Olive C.     1.10      Fix the update Robin made so it's actually working properly
 *                                 (level 3 & 4 will appear 0 blcok)  
 * 03/25/10 Judy Cheng   2.1       Add X block and xScale.
 * 03/25/10 Robin Mahony 2.2	   Updated xBlock to appear only after the 5th level, and only with a 1/30 chance.
 * ======== ============ ========= ================================================================
 *
 */

#import "Block.h"

@interface Block (private)

- (void) initializeDefaultValues;
- (void) redrawPositionOnBoard;

@end

@implementation Block

@synthesize BlockType;
@synthesize stuck;
@synthesize boardX;
@synthesize boardY;
@synthesize disappearing;
@synthesize type;

/* The method creates the block instance, selects a random number between 0 and 5 (eventually will -5 and 5, 
 where -5 will signify an X block), and depending on the result, loads the corresponding block graphic.  */
+ (Block *) newBlock:(int) lev 
{
	NSString *filename = nil, *type = nil;
	Block *temp = nil;
	
	int BlockType = arc4random() % 5 + 1;  //range from 1 to 5
	int zeroScale = arc4random() % 100;
	int negScale = arc4random() % 100;
	int xScale = arc4random() % 30;

	
	if (lev == 3 || lev == 4)
	{
		BlockType = arc4random() % 6;  /* Range of values from 0 to 5.  */
		if (BlockType == 0) 
		{
			//ensures zeros occur the least frequently
			if(zeroScale % 4 != 0)
				BlockType = arc4random() % 5 + 1; //values from 1 to 5
		}
	}
	//introduces new blocks when level is between 5 and 8
	else if (lev >= 5 && lev <= 8)
	{
		BlockType = arc4random() % 10 - 4;  /* Range from -4 to 5.  */
		
		if (BlockType == 0) 
		{
			//ensures zeros occur the least frequently
			if (zeroScale % 4 != 0)
				BlockType = arc4random() % 5 + 1; //values from 1 to 5
		}
		
		else if (BlockType < 0)
		{
			if (negScale % 2 != 0)
				BlockType = arc4random() % 5 + 1; //values from 1 to 5
			
		}
		
		else if(xScale == 1)
		{
			BlockType = 10;
		}
		
	}
	
	//increase X block frequency for high levels
	else if (lev >= 9)
	{
		BlockType = arc4random() % 10 - 4;  /* Range from -4 to 5.  */
		xScale = arc4random() % 15;
		if (BlockType == 0) 
		{
			//ensures zeros occur the least frequently
			if (zeroScale % 4 != 0)
				BlockType = arc4random() % 5 + 1; //values from 1 to 5
		}
		
		else if (BlockType < 0)
		{
			if (negScale % 2 != 0)
				BlockType = arc4random() % 5 + 1; //values from 1 to 5
			
		}
		
		else if(xScale == 1)
		{
			BlockType = 10;
		}
		
	}
	
	
	
	switch (BlockType) 
	{
		case -4:
			type = @"n4";
			break;
		case -3:
			type = @"n3";
			break;
		case -2:
			type = @"n2";
			break;
		case -1:
			type = @"n1";
			break;
		case 0:
			type = @"0";
			break;
		case 1:
			type = @"1";
			break;
		case 2:
			type = @"2";
			break;
		case 3:
			type = @"3";
			break;
		case 4:
			type = @"4";
			break;
		case 5:
			type = @"5";
			break;
		case 10:
			type = @"X";
			break;
		default:
			type = nil;
			break;
	}
	
	//Loads the correct block image for the specified value/type
	if (type) 
	{
		filename = [[NSString alloc] initWithFormat:@"%@block.pvr", type];
		temp = [self spriteWithFile:filename];
		[filename release];
		
		[temp setType: type];
		[temp initializeDefaultValues];
		[temp setBlockType: BlockType];
	}
	return temp;
}

/* This method is used to set all the default properties in a block.  */
- (void) initializeDefaultValues 
{
	/* Move the board coordinate to be top/right-oriented in order to control 
	 where the blocks will fall within the game field.  */
	[self setTransformAnchor: ccp (-64,10) ];
	[self setPosition: ccp (0,0) ];
	[self setOpacity: 255]; /* Transparency of the block  */
	[self setStuck: NO];
	[self setDisappearing: NO];
	[self setBoardX: 0];
	[self setBoardY: 0];
}

/* Sets the block position on the screen to the coordinates corresponding to the board X and Y 
 coordinates.  */
- (void) redrawPositionOnBoard 
{
	[self setPosition: COMPUTE_X_Y (boardX, boardY) ];
}

/* Moves the block one unit to the right on the board.  */
- (void) moveRight 
{
	boardX += 1;
	[self redrawPositionOnBoard];
}

/* Moves the block one unit to the left on the board.  */
- (void) moveLeft
{
	boardX -= 1;
	[self redrawPositionOnBoard];
}

/* Moves the block one unit to the bottom on the board.  */
- (void) moveDown 
{
	boardY += 1;
	[self redrawPositionOnBoard];
}

/* Moves the block one unit to the top on the board.  */
- (void) moveUp 
{
	boardY -= 1;
	[self redrawPositionOnBoard];
}

@end
